package io.github.wanggit.antrpc.gateway.starter.zk;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/** Zookeeper客户端，基本使用 {@link CuratorFramework} 的方法 */
public class ZkClient implements IZkClient {

    private final CuratorFramework curatorFramework;

    public ZkClient(ZkConfig zkConfig) {
        RetryPolicy retryPolicy =
                new ExponentialBackoffRetry(
                        zkConfig.getZkConnectRetryBaseSleepMs(),
                        zkConfig.getZkConnectMaxRetries(),
                        zkConfig.getZkConnectRetryMaxSleepMs());
        curatorFramework =
                CuratorFrameworkFactory.builder()
                        .connectString(zkConfig.getZkIps())
                        .connectionTimeoutMs(zkConfig.getZkConnectionTimeoutMs())
                        .retryPolicy(retryPolicy)
                        .build();
        curatorFramework.start();
    }

    @Override
    public CuratorFramework getCurator() {
        return curatorFramework;
    }
}
